<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Cache;
use App\Job;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        $this->shareKinds();
    }

    public  function shareKinds(){
		Cache::forget(config('site.cachenames.kind'));
        if(Cache::has(config('site.cachenames.kind'))){
            $kind = Cache::get(config('site.cachenames.kind'));
        }
        else{
            $kind = Job::getKinds();
            $tempkind = array_filter($kind, function($item){
                if($item->categoryid <= 12){
                    $str = '';
                    switch($item->categoryid){
                        case 1;     $str='marriage';        break;      //快婚
                        case 2;     $str='business';        break;      //快商
                        case 3;     $str='ad';              break;      //广告图文
                        case 4;     $str='costume';         break;      //服装设计
                        case 5;     $str='decorate';        break;      //装修
                        case 6;     $str='hotel';           break;      //酒店
                        case 7;     $str='farm';            break;      //农庄
                        case 8;     $str='wedding';         break;      //婚庆
                        case 9;     $str='amusement';       break;      //娱乐
                        case 10:    $str='car';             break;      //二手车
                        case 11:    $str='work';            break;      //快工
                        default:                            break;
                    }
                    $item->kindstr = $str;
                    $item->subkind = Array();
                    return  true;
                }else{
                    return false;
                }
            });
            foreach($tempkind as $subkey=>$subitem){
                foreach($kind as $key=>$item){
                    if($item->parentid == $subitem->categoryid){
                        array_push($tempkind[$subkey]->subkind,  [
                            'categoryid'=>$item->categoryid,
                            'name'=>$item->name,
                            'parentid'=>$item->parentid,
                            'kindstr'=>$subitem->kindstr
                        ]);
                    }
                }
            }
            Cache::put(config('site.cachenames.kind'), $tempkind, 100);
            $kind = $tempkind;
        }
        view()->share(["kind"=>$kind]);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
